﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Xml;

namespace QLDBNH_CLIENT
{
    public partial class frmChuyenban : Form
    {
        chuoi Chuoi = new chuoi();
        Sent_Receive sr = new Sent_Receive();
        DataSet chuyenban = new DataSet();
        DataTable CB = new DataTable("CB");

        string ipclient = "";
        string tbanC = "", tbanTT = "";

        public frmChuyenban()
        {
            InitializeComponent();

            //Get host name.
            string myHost = System.Net.Dns.GetHostName();

            //Get the IP from the host name.
            string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

            ipclient = myIP;
        }

        private void frmChuyenban_Load(object sender, EventArgs e)
        {
            try
            {
                frmMainForm.dataset.Tables["tbQlDatBan"].Clear();
                frmMainForm.dataset.Tables["tbQlDatBan"].ReadXml("tbQlDatBan.xml");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            try
            {
                for (int i = 0; i < frmMainForm.dataset.Tables["tbQlDatBan"].Rows.Count; i++)
                    lb_Dsban.Items.Add(frmMainForm.dataset.Tables["tbQlDatBan"].Rows[i][1].ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            //table 'CB';
            CB.Columns.Add(new DataColumn("Ban1", typeof(System.String)));
            CB.Columns.Add(new DataColumn("Ban2", typeof(System.String)));
            CB.Columns.Add(new DataColumn("ViTri", typeof(System.String)));
            chuyenban.Tables.Add(CB);

            //ghi ip va khoa nhan dang du lieu.
            DataRow cban = chuyenban.Tables["CB"].NewRow();
            cban[0] = "6";
            cban[1] = ipclient;
            chuyenban.Tables["CB"].Rows.Add(cban);

            try
            {
                frmMainForm.dataset.Tables["tbQlDatBan"].WriteXml("tbQlDatBan.xml");
                frmMainForm.dataset.Tables["tbQlDatBan"].Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                frmMainForm.dataset.Tables["tbQlDatBan"].Clear();
                frmMainForm.dataset.Tables["tbQlDatBan"].ReadXml("tbQlDatBan.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            string tenban = "";
            int flag = 0;

            lb_Dsban1.Items.Clear();
           
            //danh sach ban chua co khach.
            try
            {
                for (int i = 0; i < frmMainForm.Ban.Tables[0].Rows.Count; i++)
                {
                    if (Chuoi.c_h_string(frmMainForm.Ban.Tables[0].Rows[i][0].ToString()).CompareTo(Chuoi.c_h_string(cb_Vitri.Text)) == 0)
                    {
                        tenban = Chuoi.c_h_string(frmMainForm.Ban.Tables[0].Rows[i][1].ToString());
                        for (int j = 0; j < frmMainForm.dataset.Tables["tbQlDatBan"].Rows.Count; j++)
                        {
                            if (tenban.CompareTo(Chuoi.c_h_string(frmMainForm.dataset.Tables["tbQlDatBan"].Rows[j][1].ToString())) != 0)
                            {
                                flag = flag + 1;
                            }
                        }
                        if (flag == frmMainForm.dataset.Tables["tbQlDatBan"].Rows.Count)
                            lb_Dsban1.Items.Add(tenban);
                        flag = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            try
            {
                frmMainForm.dataset.Tables["tbQlDatBan"].WriteXml("tbQlDatBan.xml");
                frmMainForm.dataset.Tables["tbQlDatBan"].Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void bt_Dong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void bt_Chuyenban_Click(object sender, EventArgs e)
        {
            if (tbanC != "")
            {
                if (tbanTT != "")
                {
                    try
                    {
                        //ghi vao DataSet 'CB'.
                        DataRow tban = chuyenban.Tables["CB"].NewRow();
                        tban[0] = tbanC;
                        tban[1] = tbanTT;
                        tban[2] = cb_Vitri.Text;
                        chuyenban.Tables["CB"].Rows.Add(tban);

                        //chuyen DataSet 'CB' sang file CB.xml.
                        chuyenban.Tables["CB"].WriteXml("CB.txt");

                        //chuyen file CB.xml sang chuoi.
                        string cban = "";

                        StreamReader r = new StreamReader("CB.txt");
                        while (r.Peek() != -1)
                        {
                            cban += r.ReadLine();
                        }
                        r.Close();

                        //gui den Server.
                        sr.Sent(frmMainForm.ipserver, frmMainForm.portserver, cban);

                        //xoa table 'HuyBan'.
                        chuyenban.Tables["CB"].Clear();

                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("Chọn bàn thay thế");
                }
            }
            else
            {
                MessageBox.Show("Chọn bàn muốn chuyển");
            }
        }

        private void lb_Dsban_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                frmMainForm.dataset.Tables["tbQlDatBan"].Clear();
                frmMainForm.dataset.Tables["tbQlDatBan"].ReadXml("tbQlDatBan.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            int flag = 0;

            tbanC = lb_Dsban.Text;
            try
            {
                for (int i = 0; i < frmMainForm.dataset.Tables["tbQlDatBan"].Rows.Count; i++)
                {
                    if (lb_Dsban.Text.CompareTo(frmMainForm.dataset.Tables["tbQlDatBan"].Rows[i][1].ToString()) == 0)
                    {
                        if (frmMainForm.dataset.Tables["tbQlDatBan"].Rows[i][3].ToString().CompareTo("1") == 0)
                        {
                            flag = 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            if (flag == 1) 
            {
                tbanC = "";
                MessageBox.Show("Không thể chuyển vì bàn đã làm món");
            }

            //gan gia tri.
            flag = 0;

            try
            {
                frmMainForm.dataset.Tables["tbQlDatBan"].WriteXml("tbQlDatBan.xml");
                frmMainForm.dataset.Tables["tbQlDatBan"].Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void lb_Dsban1_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbanTT = lb_Dsban1.Text;
        }
    }
}
